<?php

/**
 * 购物车
 */
namespace app\order\common;

class CartCommon extends \app\member\common\MemberCommon {

    protected function index() {
        $this->pageInfo['name'] = '购物车';
        $this->setMeta('购物车');
        $this->setCrumb([
            [
                'name' => '购物车',
                'url' => url()
            ]
        ]);
        $list = target('order/Cart', 'service')->getList($this->userInfo['user_id']);
        $info = target('order/Cart', 'service')->getCart($this->userInfo['user_id']);
        $this->assign('pageList', $list);
        $this->assign('info', $info);
        $this->otherDisplay();
    }

    /**
     *
     */
    protected function submit() {
        $orderConfig = target('order/orderConfig')->getConfig();
        if (!isPost()) {
            $this->pageInfo['name'] = '结算商品';
            $this->setMeta('结算商品');
            $this->setCrumb([
                [
                    'name' => '结算商品',
                    'url' => url()
                ]
            ]);

            $urlParams = [];

            $addId = request('get', 'add_id', 0, 'intval');
            $addInfo = target('order/Address', 'service')->getAddress($this->userInfo['user_id'], $addId);
            $urlParams['add_id'] = $addId;

            $codStatus = request('get', 'cod_status', 0, 'intval');
            $urlParams['cod_status'] = $codStatus ? 1 : 0;

            $addList = target('order/OrderAddress')->loadList(['user_id' => $this->userInfo['user_id']]);
            $list = target('order/Cart', 'service')->getList($this->userInfo['user_id']);
            $info = target('order/Cart', 'service')->getCart($this->userInfo['user_id']);

            if (empty($list)) {
                $this->redirect(url('order/Address/index'));
            }
            $orderData = target('order/Order', 'service')->splitOrder($addInfo['province'], $list);

            $deliveryPrice = 0;
            $orderPrice = 0;
            foreach ($orderData as $vo) {
                $deliveryPrice += $vo['delivery_price'];
                $orderPrice += $vo['order_price'];
            }
            $this->assign('orderData', $orderData);
            $this->assign('info', $info);
            $this->assign('addInfo', $addInfo);
            $this->assign('addList', $addList);
            $this->assign('urlParams', array_filter($urlParams));
            $this->assign('pageList', $list);
            $this->assign('addId', $addInfo['add_id']);
            $this->assign('deliveryPrice', $deliveryPrice);
            $this->assign('orderPrice', $orderPrice);
            $this->assign('codStatus', $codStatus);
            $this->assign('orderConfig', $orderConfig);
            $this->otherDisplay();
        } else {
            $data = request('post');
            $codStatus = $data['cod_status'];
            $target = target('order/Order', 'service');
            $orderNos = $target->addOrder($this->userInfo['user_id'], '', $data['add_id'], $codStatus, $data['remark']);
            if (!$orderNos) {
                $this->error($target->getError());
            }
            target('order/Cart', 'service')->clear($this->userInfo['user_id']);

            if (!$codStatus) {
                $this->success('订单提交成功,请选择付款方式!', url('order/Pay/index', ['order_no' => implode('|', $orderNos)]));
            } else {
                $this->success('订单提交成功,请耐心等待发货!', url('member/Index/index'));
            }
        }
    }

    protected function num() {
        $data = request('post');
        if (empty($data['rowid'])) {
            $this->error('商品信息获取失败,请重试!');
        }
        if (!$data['qty']) {
            $this->error('商品数量不能为0!');
        }
        $cartData = [];
        $cartData['rowid'] = $data['rowid'];
        $cartData['qty'] = $data['qty'];
        if (!target('order/Cart', 'service')->update($this->userInfo['user_id'], $cartData)) {
            $this->error(target('order/Cart', 'service')->getError());
        }
        $this->success('ok');
    }

    protected function del() {
        $data = request('post');
        if (empty($data['rowid'])) {
            $this->error('商品信息获取失败,请重试!');
        }
        if (!target('order/Cart', 'service')->del($this->userInfo['user_id'], $data['rowid'])) {
            $this->error(target('order/Cart', 'service')->getError());
        }
        $this->success('ok');
    }

}